//
//  WATransactionListController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/17.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WATransactionListController.h"
#import "WATransactionCell.h"
#import "WATransactionDetailController.h"
#import "WAGoodListModel.h"
@interface WATransactionListController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSMutableArray *dataSouces;
@property (assign, nonatomic) NSInteger type;//0:  1：
@end

@implementation WATransactionListController
- (instancetype)initWtihType:(NSInteger)type
{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"WATransactionCell" bundle:nil] forCellReuseIdentifier:@"WATransactionCell"];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    self.tableView.tableFooterView = [UIView new];
}

- (void)requestData{
    [self  requestDataSucc:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WAGoodListModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        [self.dataSouces addObjectsFromArray:memberList];
        if(total <= self.dataSouces.count){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
        [self.tableView reloadEmptyDataSet];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        self.noDataStr = mes;
        [self.tableView reloadEmptyDataSet];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [WNTool show:mes];
    }];
}

- (void)requestDataSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    //transactiontype 交易类型0表示买1表示卖
    //status交易状态 0未完成，1完成
    NSInteger transactiontype = -1;//交易类型
    NSInteger status = 0;//交易状态
    NSInteger dealuserid = -1;
    NSInteger userid = -1;
    //我的买单
    if (self.type == 0) {
        transactiontype = 0;
        userid = self.userID_Int;
    }
    //交换中
    else if(self.type == 1){
        dealuserid = self.userID_Int;
        transactiontype = 1;
    }
    //我的买单
    else if(self.type == 2){
        userid = self.userID_Int;
        transactiontype = 1;
    }
    //交换中
    else{
        dealuserid = self.userID_Int;
        transactiontype = 0;
    }
    [WNBusinessRequest getTransactionListMineWithPageNum:self.pageNum transactiontype:transactiontype status:status dealuserid:dealuserid userid:userid Succ:succ fail:fail];
}

#pragma mark - UITableView Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WATransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WATransactionCell"];
    [cell configWithModel:self.dataSouces[indexPath.row] type:self.type];
    [cell setButtonActionBlock:^(WAGoodListModel * _Nonnull model) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消当前交易" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self cancelWithModel:model];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        }];
        [alert addAction:action1];
        [alert addAction:action2];
        [self presentViewController:alert animated:YES completion:nil];
    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouces.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WAGoodListModel *model = self.dataSouces[indexPath.row];
    WATransactionCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.button.titleLabel.text isEqualToString:@"已取消"] ||[cell.button.titleLabel.text isEqualToString:@"取消"] || [cell.button.titleLabel.text isEqualToString:@"已完成"]) {
        return;
    }
    WATransactionDetailController *VC = [[WATransactionDetailController alloc] initWithModel:model type:self.type];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)cancelWithModel:(WAGoodListModel *)model{
    
    
    
    [WNTool showLoadingMessage:@"正在取消"];
    [WNBusinessRequest updateTransactioninfoWithType:4 ID:model.ID status:2 image:nil Succ:^(id  _Nonnull responseObject) {
        [WNTool show:@"取消成功"];
        [self deleteCellWithModel:model];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}

- (void)deleteCellWithModel:(WAGoodListModel *)model{
    NSMutableArray *indexPaths = [NSMutableArray new];
    NSArray *temList = [self.dataSouces copy];
    for (NSInteger i = 0; i < temList.count; i++) {
        WAGoodListModel *mo = temList[i];
        if (mo.ID == model.ID) {
            [self.dataSouces removeObject:mo];
            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
            [indexPaths addObject:path];
        }
    }
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
    [self.tableView endUpdates];
}


#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.tableView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}
@end
